/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 *
 * @author MT140QuyenNB
 */

public class PhuongThreadV1 {
    
    public static String handleData(CassandraAction cassAct,String key,String changeDay,String endDay, String ColumFamlyCampaignClickCountCf,String ColumFamlyCampaignViewCountCf) throws ParseException
    {
        String retVal = "";        
        if(key.length() > 1)
        {
            retVal = getClickCampaignHour(cassAct, changeDay, changeDay, key, ColumFamlyCampaignClickCountCf, ColumFamlyCampaignViewCountCf);            
        }else {
            Debug.println(key);
            Debug.println("fail");           
        }
        return retVal;
    }
    
    public static String getClickCampaignHour(CassandraAction cassAct,String startDay,String finishDay,String key,String columnFamilyClick,String columnFamilyView) throws ParseException{

        StringBuilder sb = new StringBuilder();
        String dayStart = startDay.split("/")[0];
        String dayLast = finishDay.split("/")[0];
        String prefix = "-" +startDay.split("/")[1]+"-"+startDay.split("/")[2];

        String prefixDB = startDay.split("/")[2] + "-" + startDay.split("/")[1] + "-";

        for(int i = Integer.valueOf(dayStart) ; i <= Integer.valueOf(dayLast) ;i++)
        {
            for(int hour =0 ;hour <=23 ;hour ++)
            {
                if(hour < 10)
                {
                    String date = "0"+ hour +":00:00 " + String.valueOf(i) + prefix ;
                    long start_time = TimeUtil.longToDateWithHour(date);
                    //them 1h
                    long finish_time = start_time + 60*60*1000;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                    long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);                    
                    if(countClick >=1 || countView >=1)
                    {
                        String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                        sb.append(str).append(",");
                    }
                    //mysqldatamanager.insertDataCampainHour(key,hour, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
                } else {
                    String date = hour +":00:00 " + String.valueOf(i) + prefix ;
                    long start_time = TimeUtil.longToDateWithHour(date);
                    //them 1h
                    long finish_time = start_time + 60*60*1000;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                    long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);
                    if(countClick >=1 || countView >=1)
                    {
                        String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                        sb.append(str).append(",");
                    }
                    //mysqldatamanager.insertDataCampainHour(key,hour, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
                }
            }
        }
        return sb.toString();
    }


}

